import cv2 as cv2
import faceUtil
import numpy as np
import os
modelFileDir='./model/'
modelFileName='face'

if __name__ == '__main__':
    capture = cv2.VideoCapture('./data/1.mp4')
    cv2.namedWindow('0', cv2.WINDOW_NORMAL)

    labels=np.load(os.path.join(modelFileDir,modelFileName)+'.npy',allow_pickle=True).item()

    print(labels)
    recognizer = cv2.face.LBPHFaceRecognizer_create();
    recognizer.read(os.path.join(modelFileDir, modelFileName) + '.model')

    while True:
        flag, img = capture.read()
        if flag:
            img = cv2.flip(img, 1)
            gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
            faces = faceUtil.detectFace(img)
            names=[]
            for x, y, w, h in faces:

                # confidence值越大，检测结果越不可靠
                predict_face_id, confidence = recognizer.predict(gray[y:y+h,x:x+w])
                print('检测到'+labels.get(predict_face_id)+",置信评分:"+str(confidence))
                if confidence<80:
                    names.append(labels.get(predict_face_id))
                else:
                    names.append('unknow')


            faceUtil.drawBox(faces, img, names)
            cv2.imshow('0', img)
        if cv2.waitKey(1) == ord('q'):  # 如果点了退出键
            break;

